<template>
	<view class="container">
		<view class="header">
			<view class="header-content">
				<view class="title">订单数据</view>
			</view>
			<view class="dashboard">
				<view class="dashboard-box">
					<view class="dashboard-row">
						<view class="dashboard-item">
							<view class="value highlight">{{dashboard.today_receive}}</view>
							<view class="label">今日收款</view>
						</view>
						<view class="dashboard-item">
							<view class="value highlight">{{dashboard.today_disburse}}</view>
							<view class="label">今日支出</view>
						</view>
						<view class="dashboard-item">
							<view class="value highlight">{{dashboard.today_order_num}}</view>
							<view class="label">今日订单数</view>
						</view>
					</view>
					<view class="dashboard-row">
						<view class="dashboard-item">
							<view class="value">{{dashboard.today_activity_prize_num}}</view>
							<view class="label">今日礼品中奖</view>
						</view>
						<view class="dashboard-item">
							<view class="value">{{dashboard.today_box_prize_num}}</view>
							<view class="label">今日盲盒中奖</view>
						</view>
						<view class="dashboard-item">
							<view class="value">{{dashboard.total_receive}}</view>
							<view class="label">总收款</view>
						</view>
					</view>
					<view class="dashboard-row">
						<view class="dashboard-item">
							<view class="value">{{dashboard.total_disburse}}</view>
							<view class="label">总支出</view>
						</view>
						<view class="dashboard-item">
							<view class="value">{{dashboard.total_order_num}}</view>
							<view class="label">总订单</view>
						</view>
						<view class="dashboard-item">
							<view class="value">{{dashboard.total_activity_prize_num}}</view>
							<view class="label">总礼品中奖</view>
						</view>
					</view>
					<view class="dashboard-row">
						<view class="dashboard-item">
							<view class="value">{{dashboard.total_box_prize_num}}</view>
							<view class="label">总盲盒中奖</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 查询条件 -->
			<view class="search-box">
				<view class="search-row">
					<view class="search-item">
						<picker mode="date" :value="startDate" @change="onStartDateChange">
							<view class="picker">
								<text class="label">开始日期</text>
								<text class="value">{{startDate || '请选择'}}</text>
							</view>
						</picker>
					</view>
					<view class="search-item">
						<picker mode="date" :value="endDate" @change="onEndDateChange">
							<view class="picker">
								<text class="label">结束日期</text>
								<text class="value">{{endDate || '请选择'}}</text>
							</view>
						</picker>
					</view>
					<view class="search-item" v-if="type == 1">
						<picker :range="payStatusOptions" :value="searchForm.payStatus" @change="onPayStatusChange">
							<view class="picker">
								<text class="label">支付状态</text>
								<text class="value">{{payStatusOptions[searchForm.payStatus] || '请选择'}}</text>
							</view>
						</picker>
					</view>
				</view>
				<view class="search-row">
					<view class="search-item">
						<input type="text" v-model="searchForm.nickname" placeholder="请输入昵称" class="input" />
					</view>
					<view class="search-item">
						<input type="text" v-model="searchForm.realname" placeholder="请输入姓名" class="input" />
					</view>
					<view class="search-item">
						<input type="text" v-model="searchForm.mobile" placeholder="请输入电话" class="input" />
					</view>
				</view>
				<view class="search-row">
					<view class="search-item">
						<button @click="reset" class="btn-default">重置</button>
					</view>
					<view class="search-item">
						<button @click="search" class="btn-primary">查询</button>
					</view>
				</view>
			</view>
		</view>

		<view class="content">
			<view class="tab-group">
				<view class="tab-item" @tap="screen" data-type="1" :class="type == 1 ? 'active' : ''">
					<text>活动订单</text>
				</view>
				<view class="tab-item" @tap="screen" data-type="2" :class="type == 2 ? 'active' : ''">
					<text>推荐奖励</text>
				</view>
				<view class="tab-item" @tap="screen" data-type="3" :class="type == 3 ? 'active' : ''">
					<text>礼品订单</text>
				</view>
				<view class="tab-item" @tap="screen" data-type="4" :class="type == 4 ? 'active' : ''">
					<text>盲盒订单</text>
				</view>
			</view>

			<scroll-view scroll-y="true" @scrolltolower="lower" :style="{height: scrollViewHeight+'px'}" class="scroll-view">
				<view class="list-item" v-for="item in dataList">
					<!-- 活动订单 -->
					<view v-if="type == 1" class="order-content">
						<view class="order-header">
							<text class="order-sn">订单编号：{{item.order_sn}}</text>
							<text class="order-status" :class="item.order_status == 2 ? 'paid' : ''">{{item.order_status == 2 ? '已支付' : '未支付'}}</text>
						</view>
						<view class="info-row">
							<view class="info-content user-info">
								<image :src="item.avatar_url" class="avatar" mode="aspectFill"></image>
								<view class="user-detail">
									<text class="nickname">{{item.nickname}}</text>
								</view>
							</view>
						</view>
						<view class="info-row">
							<view class="info-label">姓名</view>
							<view class="info-content">{{item.name || '--'}}</view>
						</view>
						<view class="info-row">
							<view class="info-label">手机号</view>
							<view class="info-content">{{item.phone || '--'}}</view>
						</view>
						<view class="info-row">
							<view class="info-label">推荐人</view>
							<view class="info-content">{{item.recom_nickname || '--'}}</view>
						</view>
						<view class="info-row">
							<view class="info-label">溯源员工</view>
							<view class="info-content">{{item.clerk_nickname || '--'}}</view>
						</view>
						<view class="info-row">
							<view class="info-label">订单金额</view>
							<view class="info-content price">￥{{item.total_price}}</view>
						</view>
						<view class="info-row" v-if="item.order_status == 2">
							<view class="info-label">支付时间</view>
							<view class="info-content time">{{item.pay_time}}</view>
						</view>
					</view>

					<!-- 推荐奖励 -->
					<view v-if="type == 2" class="order-content">
						<view class="order-header">
							<text class="order-sn">奖励编号：{{item.disburse_sn}}</text>
							<text class="order-status" :class="item.status == 1 ? 'success' : (item.status == 2 ? 'fail' : '')">
								{{item.status == 0 ? '正在发放' : (item.status == 1 ? '已发放' : '发放失败')}}
							</text>
						</view>
						<view class="card-header">
							<view class="time">奖励时间：{{item.create_time}}</view>
						</view>
						<view class="card-content">
							<view class="user-info">
								<image :src="item.avatar_url" class="avatar" mode="aspectFill"></image>
								<view class="info">
									<view class="nickname">{{item.nickname}}</view>
									<view class="name-phone">{{item.name || '--'}} | {{item.phone || '--'}}</view>
								</view>
								<view class="amount">¥{{item.amount}}</view>
							</view>
							<view class="source-order" v-if="item.source">
								<view class="source-title">来源订单</view>
								<view class="source-user">
									<image :src="item.source.avatar_url" class="source-avatar" mode="aspectFill"></image>
									<view class="source-info">
										<view class="source-nickname">{{item.source.nickname}}</view>
										<view class="source-name-phone">{{item.source.name || '--'}} | {{item.source.phone || '--'}}</view>
									</view>
								</view>
							</view>
						</view>
					</view>

					<!-- 礼品订单 -->
					<view v-if="type == 3" class="order-content">
						<view class="order-header">
							<text class="order-sn">中奖编号：{{item.prize_sn}}</text>
							<text class="order-status" :class="item.status == 2 ? 'success' : (item.status == 1 ? 'fail' : '')">
								{{item.status == 0 ? '未核销' : (item.status == 1 ? '已作废' : '已核销')}}
							</text>
						</view>
						<view class="info-row">
							<view class="info-content user-info">
								<image :src="item.avatar_url" class="avatar" mode="aspectFill"></image>
								<view class="user-detail">
									<text class="nickname">{{item.nickname}}</text>
								</view>
							</view>
						</view>
						<view class="info-row">
							<view class="info-label">姓名</view>
							<view class="info-content">{{item.name || '--'}}</view>
						</view>
						<view class="info-row">
							<view class="info-label">手机号</view>
							<view class="info-content">{{item.phone}}</view>
						</view>
						<view class="info-row">
							<view class="info-label">奖品名称</view>
							<view class="info-content">{{item.prize_name}}</view>
						</view>
						<view class="info-row">
							<view class="info-label">领取状态</view>
							<view class="info-content">
								<text class="verify-status" :class="item.receive_status == 1 ? 'verified' : ''">
									{{item.receive_status == 0 ? '未领取' : '已领取'}}
								</text>
							</view>
						</view>
						<view class="info-row">
							<view class="info-label">中奖时间</view>
							<view class="info-content time">{{item.create_time}}</view>
						</view>
					</view>

					<!-- 盲盒订单 -->
					<view v-if="type == 4" class="order-content">
						<view class="order-header">
							<text class="order-sn">中奖编号：{{item.prize_sn}}</text>
							<text class="order-status" :class="item.status == 2 ? 'success' : (item.status == 1 ? 'fail' : '')">
								{{item.status == 0 ? '未核销' : (item.status == 1 ? '已作废' : '已核销')}}
							</text>
						</view>
						<view class="info-row">
							<view class="info-content user-info">
								<image :src="item.avatar_url" class="avatar" mode="aspectFill"></image>
								<view class="user-detail">
									<text class="nickname">{{item.nickname}}</text>
								</view>
							</view>
						</view>
						<view class="info-row">
							<view class="info-label">姓名</view>
							<view class="info-content">{{item.name}}</view>
						</view>
						<view class="info-row">
							<view class="info-label">手机号</view>
							<view class="info-content">{{item.phone}}</view>
						</view>
						<view class="info-row">
							<view class="info-label">奖品名称</view>
							<view class="info-content">{{item.prize_name}}</view>
						</view>
						<view class="info-row">
							<view class="info-label">中奖时间</view>
							<view class="info-content time">{{item.create_time}}</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import * as api from '@/api/staff'
	import storage from '@/utils/storage'
	export default {
		components: {},
		data() {
			return {
				scrollViewHeight: '',
				tabTop: 0,
				pageScrollTop: 0,
				dataList: [],
				page: 1,
				limit: 10,
				type: 1,
				dashboard: {},
				startDate: '',
				endDate: '',
				searchForm: {
					nickname: '',
					realname: '',
					mobile: '',
					payStatus: ''
				},
				payStatusOptions: ['全部', '未支付', '已支付'],
				filterTop: 0,
				statusBarHeight: 0
			}
		},
		onPageScroll(e) {
			this.pageScrollTop = e.scrollTop;
		},
		onReady() {
			var that = this;

			// 获取状态栏高度
			const systemInfo = uni.getSystemInfoSync();
			this.statusBarHeight = systemInfo.statusBarHeight;
			
			// 计算 scrollview 高度
			this.scrollViewHeight = uni.getSystemInfoSync().windowHeight - uni.getMenuButtonBoundingClientRect().top;
			
			// 计算筛选器位置
			this.$nextTick(() => {
				const query = uni.createSelectorQuery().in(this);
				query.select('.scroll-view').boundingClientRect(data => {
					if (data) {
						// 考虑状态栏高度
						that.filterTop = data.top + that.statusBarHeight;
					}
				}).exec();
			});
		},
		onLoad() {
			this.getDashboard();
			this.getDataOrder();
		},
		methods: {
			onStartDateChange(e) {
				this.startDate = e.detail.value;
			},
			onEndDateChange(e) {
				this.endDate = e.detail.value;
			},
			onPayStatusChange(e) {
				this.searchForm.payStatus = e.detail.value;
			},
			search() {
				this.page = 1;
				this.dataList = [];
				this.getDataOrder();
			},
			lower: function(e) {
				this.page = this.page + 1;
				this.getDataOrder();
			},
			screen(event) {
				const type = event.currentTarget.dataset.type;
				if (this.type === type) return;
				this.type = type;
				this.page = 1;
				this.dataList = [];
				this.getDataOrder();
				
				// 滚动到筛选器位置
				uni.pageScrollTo({
					scrollTop: this.filterTop,
					duration: 300
				});
			},
			getDashboard: function() {
				api.dashboard({
					activityId: storage.get('activityId')
				}).then(res => {
					this.dashboard = res.data;
				})
			},
			getDataOrder: function() {
				api.dataOrder({
					activityId: storage.get('activityId'),
					page: this.page,
					limit: this.limit,
					type: this.type,
					startDate: this.startDate,
					endDate: this.endDate,
					...this.searchForm
				}).then(res => {
					if (res.code === 200) {
						// 如果是推荐奖励类型，需要处理数据格式
						if (this.type === 2) {
							const formattedData = res.data.map(item => ({
								...item,
								// 确保字段名称一致
								avatar_url: item.avatar_url || '/upload/portrait.png',
								name: item.name || '--',
								phone: item.phone || '--',
								// 处理来源订单信息
								source: item.source ? {
									...item.source,
									avatar_url: item.source.avatar_url || '/upload/portrait.png',
									name: item.source.name || '--',
									phone: item.source.phone || '--'
								} : null
							}));
							this.dataList = this.page === 1 ? formattedData : [...this.dataList, ...formattedData];
						} else {
							this.dataList = this.page === 1 ? res.data : [...this.dataList, ...res.data];
						}
					}
				})
			},
			reset() {
				this.startDate = '';
				this.endDate = '';
				this.searchForm = {
					nickname: '',
					realname: '',
					mobile: '',
					payStatus: ''
				};
				this.page = 1;
				this.dataList = [];
				this.getDataOrder();
			}
		}
	}
</script>

<style>
	page {
		background-color: #F6F7FB;
	}

	.container {
		min-height: 100vh;
		position: relative;
	}

	.header {
		background-color: #FE963F;
		width: 100%;
		min-height: 750rpx;
		border-radius: 0 0 30rpx 30rpx;
		padding-bottom: 100rpx;
		position: relative;
		overflow: hidden;
	}

	.header::before {
		content: '';
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: url('') repeat;
		opacity: 0.1;
	}

	.header-content {
		position: relative;
		z-index: 1;
	}

	.title {
		color: #fff;
		padding: 30rpx;
		font-size: 40rpx;
		font-weight: bold;
		text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
	}

	.dashboard {
		padding: 0 20rpx;
	}

	.dashboard-box {
		background-color: rgba(255, 255, 255, 0.95);
		border-radius: 16rpx;
		padding: 20rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
		backdrop-filter: blur(10px);
	}

	.dashboard-row {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;
	}

	.dashboard-row:last-child {
		margin-bottom: 0;
	}

	.dashboard-item {
		flex: 1;
		text-align: center;
		padding: 15rpx;
		position: relative;
	}

	.dashboard-item:not(:last-child)::after {
		content: '';
		position: absolute;
		right: 0;
		top: 50%;
		transform: translateY(-50%);
		width: 2rpx;
		height: 60%;
		background-color: rgba(0, 0, 0, 0.05);
	}

	.dashboard-item .value {
		font-size: 32rpx;
		color: #333;
		font-weight: bold;
		margin-bottom: 8rpx;
	}

	.dashboard-item .value.highlight {
		color: #FE963F;
		font-size: 36rpx;
	}

	.dashboard-item .label {
		font-size: 22rpx;
		color: #999;
	}

	.search-box {
		background-color: rgba(255, 255, 255, 0.95);
		margin: 20rpx;
		padding: 15rpx;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
		backdrop-filter: blur(10px);
	}

	.search-row {
		display: flex;
		justify-content: space-between;
		margin-bottom: 10rpx;
	}

	.search-row:last-child {
		margin-bottom: 0;
	}

	.search-item {
		flex: 1;
		margin: 0 5rpx;
	}

	.search-item:first-child {
		margin-left: 0;
	}

	.search-item:last-child {
		margin-right: 0;
	}

	.search-row:last-child .search-item:first-child {
		flex: 0.4;
		width: 140rpx;
	}

	.search-row:last-child .search-item:last-child {
		flex: 1.6;
		width: 340rpx;
	}

	.picker {
		background-color: #f8f9fa;
		padding: 12rpx;
		border-radius: 12rpx;
		display: flex;
		flex-direction: column;
		transition: all 0.3s ease;
	}

	.picker .label {
		font-size: 22rpx;
		color: #999;
		margin-bottom: 4rpx;
	}

	.picker .value {
		font-size: 26rpx;
		color: #333;
	}

	.input {
		background-color: #f8f9fa;
		padding: 12rpx;
		border-radius: 12rpx;
		font-size: 26rpx;
		width: 100%;
		height: 80rpx;
		box-sizing: border-box;
		transition: all 0.3s ease;
	}

	.btn-default {
		background: #fff;
		color: #666;
		width: 100%;
		height: 70rpx;
		line-height: 70rpx;
		border-radius: 12rpx;
		font-size: 26rpx;
		font-weight: bold;
		border: 2rpx solid #ddd;
		transition: all 0.3s ease;
		margin: 0;
		padding: 0 20rpx;
	}

	.btn-default:active {
		background: #f8f8f8;
		border-color: #ccc;
		color: #333;
	}

	.btn-primary {
		background: #FE963F;
		color: #fff;
		width: 100%;
		height: 70rpx;
		line-height: 70rpx;
		border-radius: 12rpx;
		font-size: 26rpx;
		font-weight: bold;
		box-shadow: 0 4rpx 12rpx rgba(254, 150, 63, 0.3);
		transition: all 0.3s ease;
		margin: 0;
		padding: 0 20rpx;
	}

	.btn-primary:active {
		background: #e8862f;
		transform: translateY(2rpx);
		box-shadow: 0 2rpx 6rpx rgba(254, 150, 63, 0.2);
	}

	.content {
		margin-top: -100rpx;
		padding: 0 20rpx;
		position: relative;
		z-index: 2;
	}

	.tab-group {
		display: flex;
		background-color: #fff;
		border-radius: 16rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
		overflow: hidden;
	}

	.tab-item {
		flex: 1;
		text-align: center;
		font-size: 28rpx;
		color: #666;
		position: relative;
		transition: all 0.3s ease;
		display: flex;
		align-items: center;
		justify-content: center;
		min-height: 88rpx;
	}

	.tab-item text {
		pointer-events: none;
	}

	.tab-item.active {
		color: #FE963F;
		font-weight: bold;
	}

	.tab-item.active::after {
		content: '';
		position: absolute;
		bottom: 0;
		left: 50%;
		transform: translateX(-50%);
		width: 40rpx;
		height: 4rpx;
		background: linear-gradient(135deg, #FE963F 0%, #FF6B6B 100%);
		border-radius: 2rpx;
	}

	.scroll-view {
		width: 100%;
	}

	.list-item {
		background-color: #fff;
		border-radius: 16rpx;
		margin-bottom: 20rpx;
		padding: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
		transition: all 0.3s ease;
	}

	.list-item:active {
		transform: translateY(2rpx);
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}

	.order-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
		padding-bottom: 20rpx;
		border-bottom: 2rpx solid #f5f5f5;
	}

	.order-sn {
		font-size: 24rpx;
		color: #333;
		font-weight: bold;
	}

	.order-status {
		font-size: 24rpx;
		padding: 6rpx 20rpx;
		border-radius: 20rpx;
		background-color: #f5f5f5;
		color: #999;
	}

	.order-status.paid {
		background-color: #e8f5e9;
		color: #4caf50;
	}

	.order-status.success {
		background-color: #e8f5e9;
		color: #4caf50;
	}

	.order-status.fail {
		background-color: #ffebee;
		color: #f44336;
	}

	.info-row {
		display: flex;
		margin-bottom: 24rpx;
	}

	.info-label {
		width: 160rpx;
		font-size: 28rpx;
		color: #999;
	}

	.info-content {
		flex: 1;
		font-size: 28rpx;
		color: #333;
	}

	.user-info {
		display: flex;
		align-items: center;
	}

	.avatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		margin-right: 20rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
	}

	.user-detail {
		display: flex;
		flex-direction: column;
	}

	.nickname {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 4rpx;
		font-weight: bold;
	}

	.realname {
		font-size: 24rpx;
		color: #666;
		margin-bottom: 4rpx;
	}

	.mobile {
		font-size: 24rpx;
		color: #999;
	}

	.price {
		color: #f44336;
		font-weight: bold;
		font-size: 32rpx;
	}

	.verify-status {
		font-size: 24rpx;
		padding: 6rpx 20rpx;
		border-radius: 20rpx;
		background-color: #f5f5f5;
		color: #999;
	}

	.verify-status.verified {
		background-color: #e8f5e9;
		color: #4caf50;
	}

	.source-order-sn {
		display: block;
		margin-bottom: 10rpx;
		color: #666;
		font-weight: bold;
	}

	.card-header {
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid #f5f5f5;
	}

	.time {
		font-size: 28rpx;
		color: #999;
	}

	.card-content {
		padding-top: 20rpx;
	}

	.user-info {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.avatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		margin-right: 20rpx;
	}

	.info {
		flex: 1;
	}

	.nickname {
		font-size: 32rpx;
		color: #333;
		font-weight: 500;
		margin-bottom: 8rpx;
	}

	.name-phone {
		font-size: 24rpx;
		color: #999;
	}

	.amount {
		font-size: 36rpx;
		color: #FF6B6B;
		font-weight: bold;
	}

	.source-order {
		background-color: #f8f8f8;
		border-radius: 16rpx;
		padding: 20rpx;
	}

	.source-title {
		font-size: 28rpx;
		color: #666;
		margin-bottom: 16rpx;
	}

	.source-user {
		display: flex;
		align-items: center;
	}

	.source-avatar {
		width: 60rpx;
		height: 60rpx;
		border-radius: 50%;
		margin-right: 16rpx;
	}

	.source-info {
		flex: 1;
	}

	.source-nickname {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 4rpx;
	}

	.source-name-phone {
		font-size: 24rpx;
		color: #999;
	}
</style>